﻿
@page "/_Admin/DicDef/Index"
@using nERP.ViewModel._Admin.DicDefVMs
@using System.ComponentModel.DataAnnotations
@inherits BasePage
@attribute [ActionDescription("_Page._Admin.DicDef.Index", "nERP._Admin.Controllers,DicDef")]

<div style="height: var(--table-height);padding: 2px 5px 10px 5px;">
    <Table @ref="DicDefListVMdataTable" TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View" OnQueryAsync="DicDefListVMOnSearchDicDef"
           IsStriped="true" IsBordered="true" ShowRefresh="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false" ShowCardView="true"
           ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
           AllowResizing="true" ShowDefaultButtons="false" IsPagination="true" AllowDragColumn="true" ShowColumnList="true" ShowSearch="true" SearchTooltip=""
           OnResetSearchAsync="OnResetSearchAsync" SearchMode="SearchMode.Top" CollapsedTopSearch="true" ShowSearchText="false">
        <SearchTemplate>
            <ValidateForm Model="@Model">
                <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
                    <BootstrapInput @bind-Value="@Model.Searcher.DicName" />
                </Row>
            </ValidateForm>
        </SearchTemplate>
  <TableColumns>
    <TableColumn @bind-Field="@context.DicDef_DicName" Text="@WtmBlazor.Localizer["Page.字典集名"]" />
    <TableColumn @bind-Field="@context.DicDef_DicField_DicDef_ID_Count" Text="@WtmBlazor.Localizer["Page.数量"]" />
  </TableColumns>

  <RowButtonTemplate>
    @if (IsAccessable("/api/_Admin/DicDef/Edit"))
    {
        <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Edit"]"  OnClick="()=>OnEditClick(context)" />
    }
    @if (IsAccessable("/api/_Admin/DicDef/{id}"))
    {
        <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-info-circle" Color="Color.Primary" Text="@WtmBlazor.Localizer["Page.详情"]"  OnClick="()=>OnDetailsClick(context)" />
    }
    @if (IsAccessable("/api/_Admin/DicDef/BatchDelete"))
    {
        <PopConfirmButton OnConfirm="() => OnDeleteClick(context)" Size="Size.ExtraSmall" Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" 
            ConfirmButtonColor="Color.Danger" />
    }
  </RowButtonTemplate>
  <TableToolbarTemplate>
    @if (IsAccessable("/api/_Admin/DicDef/Create"))
    {
        <TableToolbarButton TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View" Icon="fa fa-plus" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Create"]"  OnClickCallback="(x)=>OnCreateClick()" />
    }
    @if (IsAccessable("/api/_Admin/DicDef/BatchDelete"))
    {
        <TableToolbarPopConfirmButton TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View"
             Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" 
            OnConfirm="@OnBatchDeleteClick" ConfirmButtonColor="Color.Danger"/>
    }
    @if (IsAccessable("/api/_Admin/DicDef/BatchEdit"))
    {
        <TableToolbarButton TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.BatchEdit"]"  OnClickCallback="(x)=>OnBatchEditClick()" />
    }
    @if (IsAccessable("/api/_Admin/DicDef/Import"))
    {
        <TableToolbarButton TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View" Icon="fa fa-tasks" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Import"]"  OnClickCallback="(x)=>OnImportClick()" />
    }
    @if (IsAccessable("/api/_Admin/DicDef/DicDefExportExcel"))
    {
        <TableToolbarButton TItem="nERP.ViewModel._Admin.DicDefVMs.DicDef_View" Icon="fa fa-arrow-circle-down" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="(x)=>OnExportClick()" IsAsync="true" />
    }
  </TableToolbarTemplate>

</Table>
</div>


@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private DicDefListVM Model = new DicDefListVM();
    private Table<nERP.ViewModel._Admin.DicDefVMs.DicDef_View> DicDefListVMdataTable;
    private Task OnResetSearchAsync(DicDef_View item)
    {
        
        item = new DicDef_View();
        return Task.CompletedTask;
    }
    private async Task DicDefListVMDoSearch()
    {
        await DicDefListVMdataTable.QueryAsync();
    }
    private async Task<QueryData<nERP.ViewModel._Admin.DicDefVMs.DicDef_View>> DicDefListVMOnSearchDicDef(QueryPageOptions opts)
    {
            
        return await StartSearch<nERP.ViewModel._Admin.DicDefVMs.DicDef_View>("/api/_Admin/DicDef/SearchDicDef", Model.Searcher, opts);
    }
    private async Task OnCreateClick()
    {
        var id = DicDefListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Create>(@WtmBlazor.Localizer["Sys.Create"], x => x.id == (id ?? ""), isMax:false) == DialogResult.Yes)
        {
            await DicDefListVMdataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(nERP.ViewModel._Admin.DicDefVMs.DicDef_View item)
    {
        if (await OpenDialog<Edit>(@WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString(), isMax:false) == DialogResult.Yes)
        {
            await DicDefListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(nERP.ViewModel._Admin.DicDefVMs.DicDef_View item)
    {
        if (await OpenDialog<Details>(@WtmBlazor.Localizer["Page.详情"], x => x.id == item.ID.ToString(), isMax:false) == DialogResult.Yes)
        {
            await DicDefListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDeleteClick(nERP.ViewModel._Admin.DicDefVMs.DicDef_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/_Admin/DicDef/batchdelete", (s) => "Sys.OprationSuccess");
        await DicDefListVMdataTable.QueryAsync();
    }

    private async Task OnBatchDeleteClick()
    {
        if (DicDefListVMdataTable.SelectedRows?.Any() == true)
        {
            await PostsData(DicDefListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList(), $"/api/_Admin/DicDef/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await DicDefListVMdataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnBatchEditClick()
    {
        if (DicDefListVMdataTable.SelectedRows?.Any() == true)
        {
            if (await OpenDialog<BatchEdit>(WtmBlazor.Localizer["Sys.BatchEdit"], x => x.ids == DicDefListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToArray(), isMax:false) == DialogResult.Yes)
            {
                await DicDefListVMdataTable.QueryAsync();
            }
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnImportClick()
    {
        var id = DicDefListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Import>(@WtmBlazor.Localizer["Sys.Import"], x => x.id == (id ?? ""), isMax:false) == DialogResult.Yes)
        {
            await DicDefListVMdataTable.QueryAsync();
        }
    }

    private async Task OnExportClick()
    {
        if (DicDefListVMdataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/_Admin/DicDef/DicDefExportExcelByIds", DicDefListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/_Admin/DicDef/DicDefExportExcel", Model.Searcher);
        }
    }

    
    protected override async Task OnInitializedAsync()
    {
        
        

        await base.OnInitializedAsync();
    }
}
